

capture program drop DD_density
program define DD_density
global bin=10
global binname=$bin
global low_ct=${cutoff}+${low}
global high_ct=${cutoff}+${high}
global ndum= ($high_ct - $low_ct )/${bin}+1

if $cutoff ==300 {
global zoom_in_c300=80        
global zoom_out_c300=470
global range_c300   "incbin${binname}>=float(${zoom_in_c300}) & incbin${binname}<=float(${zoom_out_c300})"
global range_DD    "$range_c300" 
global zoom_in_DD  =$zoom_in_c300
global zoom_out_DD =$zoom_out_c300
}
if $cutoff ==200 {
global zoom_in_c200=80  
global zoom_out_c200=260 
global numerate_c200=20
global range_c200   "incbin${binname}>=float(${zoom_in_c200}) & incbin${binname}<=float(${zoom_out_c200})"
global range_DD "  $range_c200"
global zoom_in_DD  =$zoom_in_c200
global zoom_out_DD =$zoom_out_c200
}
if $cutoff ==100 {
global zoom_in_c100=60  
global zoom_out_c100=160 
global numerate_c100=20
global range_c100   "incbin${binname}>=float(${zoom_in_c100}) & incbin${binname}<=float(${zoom_out_c100})"
global range_DD "$range_c100"
global zoom_in_DD  =$zoom_in_c100
global zoom_out_DD =$zoom_out_c100
}



use  "${density_data_dir}\density_${firm_name}_${name}_${year_name}_bin${binname}_in${data_in}_out${data_out}.dta", clear
keep if $range_DD
gen year=2002
save  "${density_data_dir}\temp\temp_2002", replace


use  "${density_data_dir}\density_${firm_name}_${name_ct}_${year_name_ct}_bin${binname}_in${data_in}_out${data_out}.dta", clear
keep if $range_DD
gen year=2000
save "${density_data_dir}\temp\temp_2000", replace




use  "${density_data_dir}\temp\temp_2002", clear
append using "${density_data_dir}\temp\temp_2000"
gen post=(year==2002)
foreach num of numlist 1 (1) $ndum {
gen treat_`num'=(incbin${binname}==${low_ct}+10*(`num'-1))
gen post_treat_`num'=post*treat_`num'
}
reg numbin${binname}   i.incbin${binname}  post   post_treat_*
predict py  
keep if year==2002
foreach num of numlist 1 (1) $ndum {
replace py=py- _b[post_treat_`num']*post_treat_`num'
}
rename py numbin${bin}_counter
save "${density_data_dir}\density_DD_${cutoff}_${firm_name}_${name}_${year_name}_${year_name_ct}_low${low}_high${high}_bin${binname}_in${zoom_in_DD}_out${zoom_out_DD}.dta", replace 
qui rm  "${density_data_dir}\temp\temp_2002.dta" 
qui rm  "${density_data_dir}\temp\temp_2000.dta" 
end




capture program drop DD_density_fig
program define DD_density_fig
global bin=10
global binname=${bin}
use "${density_data_dir}\density_DD_${cutoff}_${firm_name}_${name}_${year_name}_${year_name_ct}_low${low}_high${high}_bin${binname}_in${zoom_in_DD}_out${zoom_out_DD}.dta", clear
#delimit;
twoway 
(connected numbin${binname}     incbin${binname}, sort clcolor(green) mcolor(green) msymbol(o) msize(large))
(line numbin${binname}_counter     incbin${binname}, sort clcolor(maroon) mcolor(maroon) msymbol(o) msize(large) lpattern(dash))
,
legend(label(1 "Observed")   label(2 "Counterfactual based on 2000") size(small))
xline(${cutoff}, lcolor(black) lwidth(medthick)) 
xline(${low_ct}  ${high_ct}, lcolor(black) lwidth(thin)  lpattern(dash)) 
xlabel(${zoom_in_DD} (20)  ${zoom_out_DD}, labsize(small))
ytitle(Density, size(small) )  	
xtitle(Registered Capital(unit:10,000 RMB), size(medsmall) margin(medium) ) xtitle(, alignment(top)) 				
yscale(r(0))	ylabel(#5) 
title("Density Distribution of Registered Capital for", justification(center) color(black) size(medsmall)) 
subtitle( "${firm_name} firms in ${location} ${year_note}"  " ${cutoff_note}",  justification(center) color(black) size(med))
note("Note: bin=${bin}; Data: ASIF.",size(small) )
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
;						
graph export "${density_graph_dir}/fitfig_density_DD_${cutoff}_${firm_name}_${name}_${year_name}_${year_name_ct}.pdf", replace;
#delimit cr
end  




capture program drop DD_density_diff
program define DD_density_diff
global bin=10
global binname=${bin}
use "${density_data_dir}\density_DD_${cutoff}_${firm_name}_${name}_${year_name}_${year_name_ct}_low${low}_high${high}_bin${binname}_in${zoom_in_DD}_out${zoom_out_DD}.dta", clear
gen numbin${bin}_diff=numbin${bin} - numbin${binname}_counter 
#delimit;
twoway 
(connected numbin${binname}_diff  incbin${binname}, sort clcolor(green) mcolor(green) msymbol(o) msize(large)) if incbin${binname}>=160
,
legend(label(1 "2002-2000")    size(small))
xline(${cutoff}, lcolor(black) lwidth(medthick)) 
xline(${low_ct}  ${high_ct}, lcolor(black) lwidth(thin)  lpattern(dash)) 
xlabel(160 (20)  ${zoom_out_DD}, labsize(small))
ytitle(Difference in Density, size(small) )  	
xtitle(Registered Capital(unit:10,000 RMB), size(medsmall) margin(medium) ) xtitle(, alignment(top)) 				
yscale(r(0))	ylabel(#5) 
title("Density Distribution of Registered Capital for", justification(center) color(black) size(medsmall)) 
subtitle( "${firm_name} firms in ${location} ${year_note}"  " ${cutoff_note}",  justification(center) color(black) size(med))
note("Note: bin=${bin}; Data: ASIF.",size(small) )
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
;						
graph export "${density_graph_dir}/fitfig_density_DD_diff_${cutoff}_${firm_name}_${name}_${year_name}_${year_name_ct}.pdf", replace;
#delimit cr
end  



